#include "Mutex.h"


namespace easy2d {


	Mutex::Mutex(bool canTryLock)
	{
		int kind = canTryLock ? PTHREAD_MUTEX_ERRORCHECK : PTHREAD_MUTEX_NORMAL;
		pthread_mutexattr_init(&mAttrib);
		pthread_mutexattr_settype(&mAttrib, kind);
		pthread_mutex_init(&mMutex, &mAttrib);
	}

	Mutex::~Mutex()
	{
		pthread_mutexattr_destroy(&mAttrib);
		pthread_mutex_destroy(&mMutex);
	}

	bool Mutex::trylock()
	{
		int ret = pthread_mutex_trylock(&mMutex);
		return ret == 0;
	}

	void Mutex::lock()
	{
		pthread_mutex_lock(&mMutex);
	}

	void Mutex::unlock()
	{
		pthread_mutex_unlock(&mMutex);
	}

}	// namespace


